<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
/**
 * Description of GUsuario
 *
 * @author Luis Alberto
 */
include './libs/adodb/adodb.inc.php';
include './configuracion.php';
include 'Campania.php';
class GCampania
{
    private $db;
    private $campania = null;
    public function __construct()
    {
        $this->campania = new Campania();
    }
    public function Nuevo()
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
	$cns = "select max(cam_cod) as codigo from campania";
	$rs = $db->Execute($cns);
        if($rs)
        {
            $dato = $rs->fetchRow();
            return $dato;
        }
        return null;
    }
    public function Guardar($titu,$resu,$descr,$arte,$conte,$hora,$fech,$pubobj,$usrcod,$estcod,$tipcod,$placod)
    {        
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "insert into campania (cam_titulo, cam_resumen, cam_descripcion, cam_arte, cam_contenido, ";
        $cns .= "cam_hora, cam_fecha , cam_publico_objetivo, usr_cod, est_cod, tip_cod, pla_cod)";
        $cns .= " values ('$titu','$resu','$descr','$arte','$conte','$hora','$fech',$pubobj,$usrcod,$estcod,$tipcod,$placod)";
        //echo $cns;
        $rs = $db->Execute($cns);
        return $rs;
    }
    public function Modificar($cod,$titu,$resu,$descr,$arte,$conte,$hora,$fech,$pubobj,$usrcod,$estcod,$tipcod,$placod)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
	$cns = "update campania";
        $cns .= " set cam_titulo = '$titu', cam_resumen='$resu', cam_descripcion='$descr', cam_arte='$arte', cam_contenido='$conte', cam_hora='$hora', cam_fecha='$fech', cam_publico_objetivo=$pubobj, usr_cod=$usrcod, est_cod=$estcod, tip_cod=$tipcod, pla_cod=$placod";
        $cns .= " where (cam_cod = $cod)";
	return $rs = $db->Execute($cns);
    }
    public function Eliminar($cod)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
	$cns = "delete from campania where cam_cod = $cod";
	return $rs = $db->Execute($cns);
    }
    public function Obtener($cod)
    {
        
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
	$cns = "select * from campania where cam_cod = $cod";
	$rs = $db->Execute($cns);
        if($rs)
        {
            $dato = $rs->fetchRow();
            $this->campania->setcam_cod($dato['cam_cod']);
            $this->campania->setcam_titulo($dato['cam_titulo']);
            $this->campania->setcam_resumen($dato['cam_resumen']);
            $this->campania->setcam_descripcion($dato['cam_descripcion']);
            $this->campania->setcam_arte($dato['cam_arte']);
            $this->campania->setcam_contenido($dato['cam_contenido']);
            $this->campania->setcam_hora($dato['cam_hora']);
            $this->campania->setcam_fecha($dato['cam_fecha']);
            $this->campania->setcam_publico_objetivo($dato['cam_publico_objetivo']);
            $this->campania->setusr_cod($dato['usr_cod']);
            $this->campania->setest_cod($dato['est_cod']);
            $this->campania->settip_cod($dato['tip_cod']);
            $this->campania->setpla_cod($dato['pla_cod']);
            return $this->campania;
        }         
        return null;
    }
    public function Buscar($buscar, $campo)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        /*$cns = "select * from campania where $campo like '%$buscar%'";*/
        $cns = "select c.cam_cod, c.cam_titulo, c.cam_resumen, t.tip_descripcion, ec.est_descripcion, c.cam_publico_objetivo, c.cam_fecha , us.usr_username from campania as c";
        $cns .= " inner join tipo t on c.tip_cod = t.tip_cod";
        $cns .= " inner join estadocampania as ec on c.est_cod = ec.est_cod";
        $cns .= " inner join usuario as us ON  c.usr_cod = us.usr_cod";
        $cns .= " where ($campo like '%$buscar%')";
        //echo "$cns";
        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
        return $l;
    }
    public function Seleccionar()
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "select c.cam_cod, c.cam_titulo, c.cam_resumen, c.cam_arte ,t.tip_descripcion, ec.est_descripcion, c.cam_publico_objetivo, c.cam_fecha , us.usr_username from campania as c";
        $cns .= " inner join tipo t on c.tip_cod = t.tip_cod";
        $cns .= " inner join estadocampania as ec on c.est_cod = ec.est_cod";
        $cns .= " inner join usuario as us ON  c.usr_cod = us.usr_cod";
        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
        return $l;
    }
	public function SeleccionarLetra($letra)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        /*$cns = "select * from campania where cam_titulo like '$letra%'";*/
        $cns = "select c.cam_cod, c.cam_titulo, c.cam_resumen, t.tip_descripcion, ec.est_descripcion, c.cam_publico_objetivo, c.cam_fecha , us.usr_username from campania as c";
        $cns .= " inner join tipo t on c.tip_cod = t.tip_cod";
        $cns .= " inner join estadocampania as ec on c.est_cod = ec.est_cod";
        $cns .= " inner join usuario as us ON  c.usr_cod = us.usr_cod";
        $cns .= " where (cam_titulo like '$letra%')";

        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
        return $l;
    }

    public function Obtenertipos()
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "select * from tipo";
        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
        return $l;
    }
    public function  Obtenerestadocampanias()
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "select * from estadocampania";
        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
        return $l;
    }
    public function Obtenerplantillas()
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "select * from plantilla";
        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
        return $l;
    }
}
?>
